package com.fjwt.gz.db.config.dynamic;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
*
* DB spring bean 配置信息
* @author wg
* @site https://pt.jdyd.xyz/
* @date 2022/3/8 9:39
*/
@Configuration
public class DataSourceSpringConfig {

    /** 获取 主 从的配置信息 **/
    @ConfigurationProperties(prefix = "db-config.master")
    @Bean(name = "masterDBProp")
    public DBProp masterDBProp() { return new DBProp(); }

    @ConfigurationProperties(prefix = "db-config.slave")
    @Bean(name = "slaveDBProp")
    public DBProp slaveDBProp() { return new DBProp(); }

    @Autowired
    @Qualifier("masterDBProp")
    private DBProp masterDBProp;

    @Autowired
    @Qualifier("slaveDBProp")
    private DBProp slaveDBProp;

    @Bean(name = "dataSource")
    public DynamicDataSource dynamicDataSource() throws SQLException {

        DynamicDataSource result = new DynamicDataSource();

        // 全部db连接池
        Map dbMap = new HashMap<>();
        dbMap.put(DynamicDataSource.MASTER, fillDS(masterDBProp));
        dbMap.put(DynamicDataSource.SLAVE, fillDS(slaveDBProp));

        // 默认master
        result.setDefaultTargetDataSource(dbMap.get(DynamicDataSource.MASTER));

        result.setTargetDataSources(dbMap);
        return result;
    }

    /** 填充 datasoruce */
    private DruidDataSource fillDS(DBProp thisProp) throws SQLException {

        DruidDataSource result = new DruidDataSource();
        result.setUrl(thisProp.getUrl() != null ? thisProp.getUrl() : masterDBProp.getUrl() );
        result.setUsername(thisProp.getUsername() != null ? thisProp.getUsername() : masterDBProp.getUsername() );
        result.setPassword(thisProp.getPassword() != null ? thisProp.getPassword() : masterDBProp.getPassword() );
        result.setInitialSize(thisProp.getInitialSize() != null ? thisProp.getInitialSize() : masterDBProp.getInitialSize() );
        result.setMinIdle(thisProp.getMinIdle() != null ? thisProp.getMinIdle() : masterDBProp.getMinIdle() );
        result.setMaxActive(thisProp.getMaxActive() != null ? thisProp.getMaxActive() : masterDBProp.getMaxActive() );
        result.setMaxWait(thisProp.getMaxWait() != null ? thisProp.getMaxWait() : masterDBProp.getMaxWait() );
        result.setTestWhileIdle(thisProp.getTestWhileIdle() != null ? thisProp.getTestWhileIdle() : masterDBProp.getTestWhileIdle() );
        result.setTestOnBorrow(thisProp.getTestOnBorrow() != null ? thisProp.getTestOnBorrow() : masterDBProp.getTestOnBorrow() );
        result.setTestOnReturn(thisProp.getTestOnReturn() != null ? thisProp.getTestOnReturn() : masterDBProp.getTestOnReturn() );
        result.setTimeBetweenEvictionRunsMillis(thisProp.getTimeBetweenEvictionRunsMillis() != null ? thisProp.getTimeBetweenEvictionRunsMillis() : masterDBProp.getTimeBetweenEvictionRunsMillis() );
        result.setMinEvictableIdleTimeMillis(thisProp.getMinEvictableIdleTimeMillis() != null ? thisProp.getMinEvictableIdleTimeMillis() : masterDBProp.getMinEvictableIdleTimeMillis() );
        result.setValidationQuery(thisProp.getValidationQuery() != null ? thisProp.getValidationQuery() : masterDBProp.getValidationQuery() );
        result.setPoolPreparedStatements(thisProp.getPoolPreparedStatements() != null ? thisProp.getPoolPreparedStatements() : masterDBProp.getPoolPreparedStatements() );
        result.setMaxPoolPreparedStatementPerConnectionSize(thisProp.getMaxPoolPreparedStatementPerConnectionSize() != null ? thisProp.getMaxPoolPreparedStatementPerConnectionSize() : masterDBProp.getMaxPoolPreparedStatementPerConnectionSize() );
        result.setFilters(thisProp.getFilters() != null ? thisProp.getFilters() : masterDBProp.getFilters() );
        result.setConnectionProperties(thisProp.getConnectionProperties() != null ? thisProp.getConnectionProperties() : masterDBProp.getConnectionProperties() );
        return result;
    }
}
